/*
 * Ext JS Library 2.2.1
 * Copyright(c) 2006-2009, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// movidas globos de texto /////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////


Ext.example = function(){
    var msgCt;

    function createBox(t, s){
        return ['<div class="msg">',
                '<div class="x-box-tl"><div class="x-box-tr"><div class="x-box-tc"></div></div></div>',
                '<div class="x-box-ml"><div class="x-box-mr"><div class="x-box-mc"><h3>', t, '</h3>', s, '</div></div></div>',
                '<div class="x-box-bl"><div class="x-box-br"><div class="x-box-bc"></div></div></div>',
                '</div>'].join('');
    }
    return {
        msg : function(title, format,tim){
            if(!msgCt){
                msgCt = Ext.DomHelper.insertFirst(document.body, {id:'msg-div'}, true);
            }
            msgCt.alignTo(document, 't-t');
            var s = String.format.apply(String, Array.prototype.slice.call(arguments, 1));
            var m = Ext.DomHelper.append(msgCt, {html:createBox(title, s)}, true);
            m.slideIn('t').pause(tim).ghost("t", {remove:true});
        },

        init : function(){
            var t = Ext.get('exttheme');
            if(!t){ // run locally?
                return;
            }
            var theme = Cookies.get('exttheme') || 'aero';
            if(theme){
                t.dom.value = theme;
                Ext.getBody().addClass('x-'+theme);
            }
            t.on('change', function(){
                Cookies.set('exttheme', t.getValue());
                setTimeout(function(){
                    window.location.reload();
                }, 250);
            });

            var lb = Ext.get('lib-bar');
            if(lb){
                lb.show();
            }
        }
    };
}();

//Ext.onReady(Ext.example.init, Ext.example);
//// old school cookie functions
//var Cookies = {};
//Cookies.set = function(name, value){
//     var argv = arguments;
//     var argc = arguments.length;
//     var expires = (argc > 2) ? argv[2] : null;
//     var path = (argc > 3) ? argv[3] : '/';
//     var domain = (argc > 4) ? argv[4] : null;
//     var secure = (argc > 5) ? argv[5] : false;
//     document.cookie = name + "=" + escape (value) +
//       ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
//       ((path == null) ? "" : ("; path=" + path)) +
//       ((domain == null) ? "" : ("; domain=" + domain)) +
//       ((secure == true) ? "; secure" : "");
//};
//
//Cookies.get = function(name){
//	var arg = name + "=";
//	var alen = arg.length;
//	var clen = document.cookie.length;
//	var i = 0;
//	var j = 0;
//	while(i < clen){
//		j = i + alen;
//		if (document.cookie.substring(i, j) == arg)
//			return Cookies.getCookieVal(j);
//		i = document.cookie.indexOf(" ", i) + 1;
//		if(i == 0)
//			break;
//	}
//	return null;
//};
//
//Cookies.clear = function(name) {
//  if(Cookies.get(name)){
//    document.cookie = name + "=" +
//    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
//  }
//};
//
//Cookies.getCookieVal = function(offset){
//   var endstr = document.cookie.indexOf(";", offset);
//   if(endstr == -1){
//       endstr = document.cookie.length;
//   }
//   return unescape(document.cookie.substring(offset, endstr));
//};

///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// movidas slider molon    /////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////

Ext.ux.SliderTip = Ext.extend(Ext.Tip, {
    minWidth: 10,
    offsets : [0, -10],
    init : function(slider){
        slider.on('dragstart', this.onSlide, this);
        slider.on('drag', this.onSlide, this);
        slider.on('dragend', this.hide, this);
        slider.on('destroy', this.destroy, this);
    },

    onSlide : function(slider){
        this.show();
        this.body.update(this.getText(slider));
        this.doAutoWidth();
        this.el.alignTo(slider.thumb, 'b-t?', this.offsets);
    },

    getText : function(slider){
        return slider.getValue();
    }
});


///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// movidas seleccion de texto //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////

// Cross Browser selectionStart/selectionEnd
// Version 0.2
// Copyright (c) 2005-2007 KOSEKI Kengo
// 
// This script is distributed under the MIT licence.
// http://www.opensource.org/licenses/mit-license.php

function Selection(textareaElement) {
    this.element = textareaElement;
}

Selection.prototype.create = function() {
    if (document.selection != null && this.element.selectionStart == null) {
        return this._ieGetSelection();
    } else {
        return this._mozillaGetSelection();
    }
}

Selection.prototype._mozillaGetSelection = function() {
    return { 
        start: this.element.selectionStart, 
        end: this.element.selectionEnd 
    };
}

Selection.prototype._ieGetSelection = function() {

    this.element.focus();

    var range = document.selection.createRange();
    var bookmark = range.getBookmark();

    var contents = this.element.value;
    var originalContents = contents;
    var marker = this._createSelectionMarker();
    while(contents.indexOf(marker) != -1) {
        marker = this._createSelectionMarker();
    }

    var parent = range.parentElement();
    if (parent == null || (parent.type != "textarea" && parent.type != "text")) {
        return { start: 0, end: 0 };
    }
    range.text = marker + range.text + marker;
    contents = this.element.value;

    var result = {};
    result.start = contents.indexOf(marker);
    contents = contents.replace(marker, "");
    result.end = contents.indexOf(marker);

    this.element.value = originalContents;
    range.moveToBookmark(bookmark);
    range.select();

    return result;
   
}

Selection.prototype._createSelectionMarker = function() {
    return "##SELECTION_MARKER_" + Math.random() + "##";
}


///////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// movidas Progress bar en grid ////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////


/*
 * Ext JS Library 2.0
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://extjs.com/license
 */

/**
 * @class Ext.grid.ProgressBarSelectionModel
 * @extends Ext.grid.RowSelectionModel
 * A custom selection model that renders a column of progress bar showing status
 * @constructor
 * @param {Object} config The configuration options
 * @autor Ximosoft
 */
Ext.grid.ProgressBarSelectionModel = Ext.extend(Ext.grid.RowSelectionModel, {
    /**
     * @cfg {String} header Any valid text or HTML fragment to display in the header cell for the row
     * number column (defaults to '').
     */
    header: "",
    sortable: true,
    fixed:true,
    dataIndex: '',
    id: 'progress-grid',
	text: '%',
	baseCls: 'x-progress',
	colored: true,
	initEvents : function(){
        Ext.grid.ProgressBarSelectionModel.superclass.initEvents.call(this);
	},
  
    // private
    renderer : function(v, p, record, w){
		var text_post = '%';
		
		if(this.text){
            text_post = this.text;
        }
		var text_front;
		var text_back;
		
		text_front = (v <55)?'':v+text_post;
		text_back = (v >=55)?'':v+text_post;		
		
		var style ='';
		this.colored = true;
		if (this.colored == true)
		{
			if (v <= 100 && v >66) style='-green';
			if (v < 67  && v >33) style='-orange';
			if (v < 34 ) style='-red';
		}
		
		return String.format('<div class="x-progress-wrap"><div class="x-progress-inner"><div class="x-progress-bar{0}" style="width:{1}%;"><div class="x-progress-text" style="width:100%;">{2}</div></div><div class="x-progress-text x-progress-text-back" style="width:100%;">{3}</div></div></div>',style,v,text_front,text_back);		

    }
});
Ext.reg('progress-grid', Ext.grid.ProgressBarSelectionModel);


///////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////                              ////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////

